#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

using hMailServer;

#endregion

namespace MT.Mail.WebClient
{
    public partial class MainForm : Form
    {
        #region Form Events 
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                //TODO: add a resource description
                sbpUser.Text = "Username :" + Context.Session["userName"].ToString();
                
                //Load all imapfolder for this user
                LoadImapFolder();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
        #endregion

        #region Private Methods 
        private void LoadImapFolder()
        {
            try
            {
                MT.Mail.Service.MailService ms = new MT.Mail.Service.MailService();

                IMAPFolders folders = ms.GetFolders(Context.Session["userName"].ToString(), Context.Session["password"].ToString());

                for (int i = 0; i < folders.Count; i++)
                {
                    IMAPFolder folder = folders[i];

                    //Create a root node
                    TreeNode node = new TreeNode();
                    node.Name = folder.ID.ToString();
                    node.Text = folder.Name;

                    SubFolders(folder.SubFolders, node);

                    treeFolder.Nodes.Add(node);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void SubFolders(IMAPFolders folders, TreeNode nodeRoot)
        {
            try
            {
                for (int i = 0; i < folders.Count; i++)
                {
                    IMAPFolder folder = folders[i];

                    //Create a root node
                    TreeNode node = new TreeNode();
                    node.Name = folder.ID.ToString();
                    node.Text = folder.Name;

                    SubFolders(folder.SubFolders, node);

                    nodeRoot.Nodes.Add(node);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Menu Events 
        private void mnuLogOff_Click(object sender, EventArgs e)
        {
            Context.Session.IsLoggedOn = false;
            Context.MainForm = null;
        }
        #endregion
    }
}